package com.guinea.core.web;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.util.MessageResources;
import org.apache.struts.util.RequestUtils;

import com.guinea.core.service.EntityService;
import com.guinea.core.utils.StringUtils;

public abstract class StrutsAjaxAction extends StrutsSimpleAction {

	public abstract EntityService getEntityService();

	public abstract String entity2json(Object object);

	protected abstract String saveWrapper(String text);

	/**
	 * 取得对象列表
	 */
	public ActionForward ajaxList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		List list = getEntityService().get();
		String result = collection2json(list);
		renderText(response, "{\"data\":"+result+"}");
		return null;
	}

	/**
	 * 保存前的验证.
	 */
	public ActionForward ajaxSaveValidator(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		initFirst(form,request);
		if (!validateForm(mapping, form, request))
			return handleFormErrors(mapping, form, request, response);
		return null;
	}

	/**
	 * 保存对象的Action函数.
	 */
	public ActionForward ajaxSave(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		doInitEntity(request,form);

		doSaveEntity(request,form);
		
		renderHtml(response,
				saveWrapper("{\"message\":\"保存成功。\"}"));
		return null;
	}
	
	/**
	 * erase验证.
	 */
	public ActionForward ajaxEraseValidator(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		initFirst(form,request);
		if (!validateEntity())
			return handleEntityMissing(mapping, form, request, response);
		return null;
	}

	/**
	 * 标记删除单个对象的Action函数.
	 */
	public ActionForward ajaxErase(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		doEraseEntity(request);

		renderText(response, "{\"message\":\"删除成功。\"}");
		return null;
	}

	/**
	 * view验证.
	 */
	public ActionForward ajaxViewValidator(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		initFirst(form,request);
		if (!validateEntity())
			return handleEntityMissing(mapping, form, request, response);
		return null;
	}
	
	/**
	 * 取得对象
	 */
	public ActionForward ajaxView(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		renderText(response, "{\"data\":"+entity2json(entity)+"}");
		
		return null;
	}

	protected String collection2json(Collection collection) {
		String result = "[";
		for (Iterator iter = collection.iterator(); iter.hasNext();) {
			result += entity2json(iter.next());
			if (iter.hasNext())
				result += ", ";
		}
		result += "]";
		return result;
	}

	protected ActionForward handleEntityMissing(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		renderText(response, "{\"error\":\"找不到记录!\"}");
		return null;
	}

	protected ActionForward handleSaveEntityMissing(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		renderHtml(response, saveWrapper("{\"error\":\"找不到记录!\"}"));
		return null;
	}

	protected ActionForward handleFormErrors(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		MessageResources messageResources = getResources(request);
		Locale userLocale = RequestUtils.getUserLocale(request, null);
		String result = "{\"error\":[";
		for (Iterator iter = formErrors.get(); iter.hasNext();) {
			ActionMessage m = (ActionMessage) iter.next();
			String message = messageResources.getMessage(userLocale,m.getKey(),m.getValues());
			if(StringUtils.isEmpty(message)) continue;
			result += "\"" + message + "\"";
			if (iter.hasNext())
				result += ",";
		}
		result += "]}";

		renderHtml(response, saveWrapper(result));
		return null;
	}

}
